﻿package frEngine.animateControler.particleControler
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.core.*;
    import frEngine.event.*;
    import frEngine.loaders.resource.info.*;
    import frEngine.shader.filters.*;

    public class ParticleFileData extends EventDispatcher
    {
        public var targetSurface:FrSurface3D;
        public var particleParams:ParticleParams;
        public var multyEmmiter:Vector.<IMultyEmmiterType>;
        private var _dataTexture:Texture3D;
        public var targetInfo:Info3dBase;
        public static const defaultParticleTextue:Texture3D = initDefaultTexture();

        public function ParticleFileData()
        {
            return;
        }// end function

        public function builderSurfaceImp(param1:ParticleParams) : void
        {
            var _loc_2:BitmapData = null;
            var _loc_3:ByteArray = null;
            this.particleParams = param1;
            if (Global3D.useGpuParticle)
            {
                this.targetSurface = this.createGpuBuffer3D(this.targetSurface, this.particleParams);
                var _loc_4:* = this.particleParams.indexVect;
                this.targetSurface.indexVector = this.particleParams.indexVect;
            }
            else
            {
                this.targetSurface = this.createCpuBuffer3D(this.targetSurface, this.particleParams);
                var _loc_4:* = this.particleParams.indexVect;
                this.targetSurface.indexVector = this.particleParams.indexVect;
            }
            if (this.particleParams.useAlpha || this.particleParams.useColor || this.particleParams.offsetSmoothData)
            {
                _loc_2 = this.particleParams.getColorBitmapdata();
                if (_loc_2 == null)
                {
                    _loc_2 = new BitmapData(ParticleParams.bitmapWidth, ParticleParams.bitmapHeight, false, 16777215);
                    _loc_3 = this.particleParams.colorBitmapdataBytes;
                    _loc_3.position = 0;
                    _loc_3.endian = Endian.LITTLE_ENDIAN;
                    _loc_2.setPixels(_loc_2.rect, _loc_3);
                }
                this.dateaTexture.request = _loc_2;
            }
            FrEventDispatcher.instance.proxyDispatchEvent(this, Engine3dEventName.InitComplete);
            return;
        }// end function

        private function createCpuBuffer3D(param1:FrSurface3D, param2:ParticleParams) : FrSurface3D
        {
            if (!param1)
            {
                param1 = new FrSurface3D("");
            }
            else
            {
                param1.clear();
            }
            param1.targetInfo = this.targetInfo;
            var _loc_3:* = param2.surfaceData;
            var _loc_4:* = "" + FilterName_ID.POSITION_ID + ":4:true";
            var _loc_5:* = _loc_4.split(":");
            var _loc_6:* = param1.addVertexData(_loc_5[0], _loc_5[1], _loc_5[2], null);
            _loc_6.vertexVector = _loc_3[_loc_4];
            _loc_4 = "" + FilterName_ID.UV_ID + ":2:true";
            _loc_5 = _loc_4.split(":");
            _loc_6 = param1.addVertexData(_loc_5[0], _loc_5[1], _loc_5[2], null);
            _loc_6.vertexVector = _loc_3[_loc_4];
            _loc_4 = "" + FilterName_ID.PARAM0_ID + ":4:true";
            _loc_5 = _loc_4.split(":");
            _loc_6 = param1.addVertexData(_loc_5[0], _loc_5[1], _loc_5[2], null);
            _loc_6.vertexVector = _loc_3[_loc_4];
            return param1;
        }// end function

        private function createGpuBuffer3D(param1:FrSurface3D, param2:ParticleParams) : FrSurface3D
        {
            var _loc_4:String = null;
            var _loc_5:Array = null;
            var _loc_6:FrVertexBuffer3D = null;
            if (!param1)
            {
                param1 = new FrSurface3D("");
            }
            else
            {
                param1.clear();
            }
            param1.targetInfo = this.targetInfo;
            var _loc_3:* = param2.surfaceData;
            for (_loc_4 in _loc_3)
            {
                
                _loc_5 = _loc_4.split(":");
                _loc_6 = param1.addVertexData(_loc_5[0], _loc_5[1], _loc_5[2], null);
                _loc_6.vertexVector = _loc_3[_loc_4];
            }
            return param1;
        }// end function

        public function get dateaTexture() : Texture3D
        {
            if (this.particleParams.useColor || this.particleParams.useAlpha || this.particleParams.offsetSmoothData)
            {
                if (!this._dataTexture)
                {
                    this._dataTexture = new Texture3D(null, Texture3D.MIP_NONE);
                    this._dataTexture.loadedAndUpload = true;
                }
                return this._dataTexture;
            }
            else
            {
                return defaultParticleTextue;
            }
        }// end function

        public function dispose() : void
        {
            if (this.targetSurface)
            {
                this.targetSurface.clear();
            }
            return;
        }// end function

        private static function initDefaultTexture() : Texture3D
        {
            var _loc_1:* = new BitmapData(ParticleParams.bitmapWidth, ParticleParams.bitmapHeight, false, 16777215);
            var _loc_2:* = new Texture3D(_loc_1, Texture3D.MIP_NONE);
            _loc_2.canDispose = false;
            return _loc_2;
        }// end function

    }
}
